package Config

import (
    "encoding/json"
    "fmt"
    "os"
    "strings"
    "strconv"
)

type Business struct {
	RelationName string `json:"relation_name" ini:"relation_name"`
	RelationId string `json:"relation_id" ini:"relation_id"`
	EntityClassName string `json:"entity_class_name" ini:"entity_class_name"`
	EntityClassId string `json:"entity_class_id" ini:"entity_class_id"`
}

type DbConfig struct {
	Host string `json:"host" ini:"host"`
	Port int `json:"port" ini:"port"`
	User string `json:"user" ini:"user"`
	Pwd string `json:"pwd" ini:"pwd"`
	DatabaseName string `json:"database" ini:"database"`
}

type Config struct {
	BusinessConfig Business `json:"business" ini:"business"`
	Db DbConfig `json:"db_config" ini:"db_config"`
}

var (
	CONFIG = new(Config)
	RelationMap = make(map[int]string)
	EntityClassMap = make(map[int]string) 
)

func init() {
	// 打开文件
	file, _ := os.Open("config.json")
	// 关闭文件
	defer file.Close()
	//NewDecoder创建一个从file读取并解码json对象的*Decoder，解码器有自己的缓冲，并可能超前读取部分json数据。
	decoder := json.NewDecoder(file)
	//Decode从输入流读取下一个json编码值并保存在v指向的值里
	err := decoder.Decode(&CONFIG)
	if err != nil {
		panic(err)
	}
	fmt.Println(CONFIG)
	names := strings.Split(CONFIG.BusinessConfig.RelationName,"|")
	ids := strings.Split(CONFIG.BusinessConfig.RelationId,"|")
	for i := 0; i < len(names); i++ {
		id, _ := strconv.Atoi(ids[i])
		RelationMap[id] = names[i]
	}
	class_ids := strings.Split(CONFIG.BusinessConfig.EntityClassId,"|")
	class_names := strings.Split(CONFIG.BusinessConfig.EntityClassName,"|")
	for i := 0; i < len(class_ids); i++ {
		id, _ := strconv.Atoi(class_ids[i])
		EntityClassMap[id] = class_names[i]
	}
}
